#### REPLICATION MATERIALS FOR ''BENCHMARKING PANDEMIC RESPONSE: HOW THE UK'S COVID-19 VACCINE ROLL-OUT AFFECTED POPULAR SUPPORT FOR THE EU''
###### Authors: Irene Rodríguez, Toni Rodon, Asli Unan, Lisa Herbig, Heike Klüver & Theresa Kuhn
###### Journal: British Journal of Political Science

###### FIGURES AND TABLES IN BODY OF MANUSCRIPT

# Loading libraries
if (!require("pacman")) install.packages("pacman")
pacman::p_load(kableExtra, marginaleffects, modelsummary, knitr, estimatr, tidyverse, 
               cobalt, knitr, RColorBrewer, janitor, lubridate, readstata13, countrycode, rdrobust, magick, 
               jtools, ggiraphExtra, sjPlot, gridExtra, data.table, pastecs, MatchIt, readxl)

# Set working directory
setwd("~/Replication materials")


data_raw <- read.dta13("Raw data/ZA7750_v2-0-0.dta")


# clean data
data <- data_raw %>%
  
  # select & rename variabales
  select(
    
    # study info
    uniqid, 
    isocntry,
    country,
    capi_cawi,
    
    # weights
    "wgt" = w1,
    "wgt_de" = w3,
    
    # country specific regions
    starts_with("region"),
    
    # interview date
    "date" = p1,
    
    # demographics
    "age" = d11,
    "gender" = d10,
    "eduy" = d8,
    "lr_self" = d1,
    "unemployment" = d15a,
    "subjective_class" = d63,
    "living_area" = d25,
    "collar" = d15a,
    "employment" = d15a,
    
    # outcomes
    "eu_support" = qa8,
    "eu_benefits" = qa10,
    "eu_positive" = d78,
    "eu_health_issues" = qb4_1,
    "eu_ss_issues" = qb4_2,
    "eu_education_issues" = qb4_3,
    "eu_equality_issues" = qb4_4,
    "eu_climate_issues" = qb4_5,
    "eu_jobs_issues" = qb4_6,
    "eu_digitalisation_issues" = qb4_7,
    "eu_workingconditions_issues" = qb4_8,
    "eu_right_direction" = d73_2,
    "eu_future_optimistic" = sd17,
    "eu_democracy" = sd18b,
    "eu_help" = qd6_1,
    "eu_coord" = qd6_2,
    
    # Interest in poiltics
    "national_interest" = d71_1,
    "european_interest" = d71_2,
    "local_interest" = d71_3
    
    
  ) %>%
  
  # unite all regional variables into one
  unite(region, starts_with("region"), remove = TRUE, na.rm = TRUE) %>%
  
  # fix country names
  mutate(
    
    ### treatment, country & weight ###
    
    # fix country name and iso identifier
    country = ifelse(str_detect(country, "Germany"), "Germany", as.character(country)),
    iso2c = country %>% countryname(destination = "iso2c"),
    country = iso2c %>% countrycode(origin = "iso2c", destination = "country.name"),
    
    # fix weight variable
    wgt = ifelse(iso2c == "DE", wgt_de, wgt),
    
    # correct date format
    date = dmy(date),
    
    # treatment variable
    treat = ifelse(date > as.Date("2020-12-08"), 1, 0),
    
    # day indicator
    day = as.numeric(date - min(date)),
    
    # add cluster
    cluster = as.factor(paste(date, region, sep = "_")),
    
    ### controls ###
    
    # recode age
    age_r = fcase(age < 36, "young",
                  age > 35 & age < 60, "middle-aged",
                  age > 59, "old"),
    
    # recode gender
    gender = ifelse(gender == "Man", 1, 0),
    
    # recode education
    eduy = ifelse(eduy %in% c(0, 997:999), NA, eduy),
    
    # left-right selftplacement
    lr_self = ifelse(as.numeric(lr_self) %in% c(11, 12), NA, as.numeric(lr_self)),
    
    ideology_r = fcase(lr_self == 1 | lr_self == 2, "far left",
                       lr_self == 3 | lr_self == 4, "center-left",
                       lr_self == 5, "center",
                       lr_self == 6 | lr_self == 7 | lr_self == 8, "center_right",
                       lr_self == 9 | lr_self == 10, "far right"),
    
    # unemployment
    unemployment = ifelse(as.numeric(unemployment) == 3, 1, 0),
    
    # white / blue collar
    collar = fcase(collar == "Responsible for ordinary shopping, etc." |
                     collar == "Professional (lawyer, etc.)" |
                     collar == "Owner of a shop, craftsmen, etc." |
                     collar == "Business proprietors, etc." |
                     collar == "Employed professional /employed doctor, etc.)" |
                     collar == "General management, etc." |
                     collar == "Middle management, etc." |
                     collar == "Employed position, at desk" |
                     collar == "Employed position, travelling" |
                     collar == "Supervisor", 1,
                   collar == "Farmer" |
                     collar == "Fisherman" |
                     collar == "Employed position, service job" |
                     collar == "Skilled manual worker" |
                     collar == "Unskilled manual worker, etc.", 0), # 1 = white collar, 0 = blue collar
    
    employment = fcase(employment == "Unemployed, temporarily not working", "Unemployed",
                       employment == "Farmer" |
                         employment == "Fisherman" |
                         employment == "Employed position, service job" |
                         employment == "Skilled manual worker" |
                         employment == "Unskilled manual worker, etc.", "Blue collar",
                       employment == "Responsible for ordinary shopping, etc." |
                         employment == "Professional (lawyer, etc.)" |
                         employment == "Owner of a shop, craftsmen, etc." |
                         employment == "Business proprietors, etc." |
                         employment == "Employed professional /employed doctor, etc.)" |
                         employment == "General management, etc." |
                         employment == "Middle management, etc." |
                         employment == "Employed position, at desk" |
                         employment == "Employed position, travelling" |
                         employment == "Supervisor", "White collar"),
    
    employment = factor(employment, levels = c("Unemployed", "Blue collar", "White collar")),
    
    # subjective social class
    subjective_class = ifelse(as.numeric(subjective_class) %in% c(6:9), NA, as.numeric(subjective_class)),
    
    # living area
    living_area = ifelse(as.numeric(living_area) == 4, NA, as.numeric(living_area)),
    
    # interest in politics
    national_interest = ifelse(as.numeric(national_interest) == 4, NA, as.numeric(national_interest)),
    european_interest = ifelse(as.numeric(european_interest) == 4, NA, as.numeric(european_interest)),
    local_interest = ifelse(as.numeric(local_interest) == 4, NA, as.numeric(local_interest)),
    
    ### outcomes ###
    
    # eu support
    eu_support = ifelse(eu_support == "Don't know (Spontaneous)", NA, as.numeric(eu_support)),
    eu_support = (eu_support-5) * -1,
    
    # country benefits from EU membership
    eu_benefits = ifelse(eu_benefits == "Don't know (Spontaneous)", NA, as.numeric(eu_benefits)),
    eu_benefits = (eu_benefits - 3) * -1,
    
    # eu good in general positive/negative
    eu_positive = ifelse(eu_positive == "Don't know (Spontaneous)", NA, as.numeric(eu_positive)),
    eu_positive = (eu_positive-6) * -1,
    
    # more eu decision making in health issues
    eu_health_issues = fcase(
      eu_health_issues == "More decision - making at a European level", 3,
      eu_health_issues == "No change is needed/as it is now (Spontaneous)", 2,
      eu_health_issues == "Less decision - making at a European level", 1
    ),
    
    eu_ss_issues = fcase(
      eu_ss_issues == "More decision - making at a European level", 3,
      eu_ss_issues == "No change is needed/as it is now (Spontaneous)", 2,
      eu_ss_issues == "Less decision - making at a European level", 1
    ),
    
    eu_education_issues = fcase(
      eu_education_issues == "More decision - making at a European level", 3,
      eu_education_issues == "No change is needed/as it is now (Spontaneous)", 2,
      eu_education_issues == "Less decision - making at a European level", 1
    ),
    
    eu_equality_issues = fcase(
      eu_equality_issues == "More decision - making at a European level", 3,
      eu_equality_issues == "No change is needed/as it is now (Spontaneous)", 2,
      eu_equality_issues == "Less decision - making at a European level", 1
    ),
    
    eu_climate_issues = fcase(
      eu_climate_issues == "More decision - making at a European level", 3,
      eu_climate_issues == "No change is needed/as it is now (Spontaneous)", 2,
      eu_climate_issues == "Less decision - making at a European level", 1
    ),
    
    eu_jobs_issues = fcase(
      eu_jobs_issues == "More decision - making at a European level", 3,
      eu_jobs_issues == "No change is needed/as it is now (Spontaneous)", 2,
      eu_jobs_issues == "Less decision - making at a European level", 1
    ),
    
    eu_digitalisation_issues = fcase(
      eu_digitalisation_issues == "More decision - making at a European level", 3,
      eu_digitalisation_issues == "No change is needed/as it is now (Spontaneous)", 2,
      eu_digitalisation_issues == "Less decision - making at a European level", 1
    ),
    
    eu_workingconditions_issues = fcase(
      eu_workingconditions_issues == "More decision - making at a European level", 3,
      eu_workingconditions_issues == "No change is needed/as it is now (Spontaneous)", 2,
      eu_workingconditions_issues == "Less decision - making at a European level", 1
    ),
    
    # things in the eu are going in the right/wrong direction
    eu_right_direction = fcase(
      eu_right_direction == "Things are going in the right direction", 3,
      eu_right_direction == "Neither the one nor the other (SPONTANEOUS)", 2,
      eu_right_direction == "Things are going in the wrong direction", 1
    ),
    
    # optimistic/pessimistic about eu future
    eu_future_optimistic = ifelse(eu_future_optimistic == "Don't Know", NA, as.numeric(eu_future_optimistic)),
    eu_future_optimistic = (eu_future_optimistic-5) * -1,
    
    # satisfaction with democracy in eu
    eu_democracy = ifelse(eu_democracy == "Don't know", NA, as.numeric(eu_democracy)),
    eu_democracy = (eu_democracy-5) * -1,
    
    # eu coordination helped country or not
    
    eu_help = ifelse(eu_help == "Don't know (Spontaneous)", NA, as.numeric(eu_help)),
    eu_help = (eu_help-5) * -1,
    
    # more coordination for the eu to coordinate crises in the future
    
    eu_coord = ifelse(eu_coord == "Don't know (Spontaneous)", NA, as.numeric(eu_coord)),
    eu_coord = (eu_coord-5) * -1
    
  ) %>%
  
  mutate(
    
    national_interest = (national_interest-4)*-1,
    european_interest = (european_interest-4)*-1,
    local_interest = (local_interest-4)*-1
    
  ) %>%
  
  # drop unnecessary variables
  select(-wgt_de) %>% # new database including news sentiment and new treatment
  mutate(treat2 = ifelse(country == "Austria" & date > as.Date("2020-12-06") |
                           country == "Bulgaria" & date > as.Date("2020-12-07") |
                           country == "Cyprus" & date > as.Date("2020-12-06") |
                           country == "France" & date > as.Date("2020-12-06") |
                           country == "Germany" & date > as.Date("2020-12-08") |
                           country == "Ireland" & date > as.Date("2020-12-06") |
                           country == "Italy" & date > as.Date("2020-12-07") |
                           country == "Luxembourg" & date > as.Date("2020-12-08") |
                           country == "Netherlands" & date > as.Date("2020-12-08") |
                           country == "Poland" & date > as.Date("2020-12-07") |
                           country == "Portugal" & date > as.Date("2020-12-07") |
                           country == "Spain" & date > as.Date("2020-12-06"), 1,0),
         treat2 = ifelse(country == "Belgium" | 
                           country == "Croatia" |
                           country == "Czechia" |
                           country == "Denmark" |
                           country == "Estonia" |
                           country == "Finland" |
                           country == "Greece" |
                           country == "Hungary" |
                           country == "Latvia" |
                           country == "Lithuania" |
                           country == "Malta" |
                           country == "Romania" |
                           country == "Slovakia" |
                           country == "Slovenia" |
                           country == "Sweden", NA, treat2)) %>%
  mutate(employment_r = fcase(employment == "Unemployed", 0,
                              employment == "Blue collar", 1,
                              employment == "White collar", 2))

## polarisation


polarisation_raw <- read_xlsx("Raw data/41304_2022_400_MOESM3_ESM.xlsx")

eb_countries <- list("Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus",
                     "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Germany", 
                     "Greece", "Hungary", "Italy", "Ireland", "Latvia", "Lithuania", "Luxembourg", "Malta",
                     "Netherlands", "Poland", "Portugal", "Romania",
                     "Slovakia", "Slovenia", "Spain", "Sweden")

polarisation <- polarisation_raw[,-1] %>%
  filter(Country %in% eb_countries) %>%
  select(country = Country,
         year = Year,
         elite_aff = `Elite Affective`,
         mass_aff = `Mass Affective`,
         elite_ideo = `Elite Ideological`,
         mass_ideo = `Mass Ideological`,
         total_pol = `Total Polarisation`,
         total_cert = `Total Certainty`,
         types = `Number of Types Present`) %>%
  filter(year == '2020') %>%
  mutate(country = ifelse(country == "Czech Republic", "Czechia", country))

## merge with polarisation

data <- merge(data, polarisation, by = "country", all = TRUE)

# regions

eastern_countries <- list("Bulgaria", "Romania")
western_countries <- list("Belgium", "Luxembourg", "France", "Netherlands")
northern_countries <- list("Ireland", "Denmark", "Finland", "Sweden", "Estonia", "Latvia", "Lithuania")
southern_countries <- list("Croatia", "Portugal", "Spain", "Italy", "Malta", "Cyprus", "Greece")
central_countries <- list("Germany", "Poland", "Czechia", "Slovakia", "Hungary", "Slovenia", "Austria")

data <- data %>%
  mutate(europe = fcase(country %in% eastern_countries, "eastern",
                        country %in% western_countries, "western",
                        country %in% northern_countries, "northern",
                        country %in% southern_countries, "southern",
                        country %in% central_countries, "central"))



hospital_data_raw <- read_xlsx("Raw data/hospitals.xlsx")

hospital_data <- hospital_data_raw %>%
  select(c(country, date, value))%>%
  mutate(date = as.Date(date))

data <- merge(data, hospital_data, by = c('country', 'date'), all = T)
data <- data %>%
  rename(hospital_occupancy = `value`)
data <- data[!duplicated(data$uniqid), ]

save(data, file = "Data/data.RData")
